﻿using Super_Management_Mayhem.Content.Spritesheet.Player;
using Super_Management_Mayhem.Screens.Playing.AttackModel;
using Super_Management_Mayhem.Screens.Playing.Items;
using System.Linq;
using Transformable_Engine_v2.Engine.Animation.PositionAnimation;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Players.Native
{
    public class Arrow : ImageContainer, IAttacker
    {
        static readonly TE_Rectangle HitBox = new TE_Rectangle(0, 0, 0.5f, 0.5f, TE_Rectangle.Alignment.CenterMiddle).ToGridBound();

        private const float ARROW_SPEED = 20;

        private bool _isAttacking;

        private readonly BaseAttack _attack;
        private readonly TE_Vector2 _direction;

        public Arrow(TE_Vector2 position, TE_Vector2 direction, int damage, Team team)
        {
            Position = position;
            Sprite = NativeSheet.GetSprite(NativeSheet.Objects.Arrow);
            Origin = TE_Rectangle.Alignment.CenterRight;
            Rotation = direction.Angle.AsTwoPiMultiple();

            _attack = new UnidirectionalAttack(BaseAttack.TargetTeam.EnemyTo, team, this, damage, 200, HitBox, direction.ToUnit(), 12);

            _direction = direction.ToUnit();

            if (direction.X < 0)
            {
                Position += new TE_Vector2(-18, -10);
            }
            else if (direction.X > 0)
            {
                Position += new TE_Vector2(18, -10);
            }
            else if (direction.Y < 0)
            {
                Position += new TE_Vector2(2, -26);
            }
            else if (direction.Y > 0)
            {
                Position += new TE_Vector2(2, 10);
            }

            AddAnimations(new PositionUniformLinearAnimation(0, direction.ToUnit() * ARROW_SPEED));

            _isAttacking = true;
        }

        public override void FixedStepUpdate(System.TimeSpan gameTime)
        {
            if (_isAttacking)
            {
                _attack.Attack(AbsolutePosition);
            }

            foreach (Warp warp in Playscreen.Map.GetWarp().Where(warp => CollisionHelper.Intersects(warp.BoundingBox, Position)))
            {
                Position = warp.WarpPlayer().Offset(8, 8);
            }

            if (_isAttacking && Playscreen.Map.CollidesWithWall(AbsolutePosition))
            {
                Position += _direction * ARROW_SPEED * 0.25f;
                ClearAnimation();
                _isAttacking = false;
                Sprite = NativeSheet.GetSprite(NativeSheet.Objects.ArrowCropped);
                Origin = TE_Rectangle.Alignment.CenterRight;
                Die(30000);
            }

            base.FixedStepUpdate(gameTime);
        }

        public IAttacker GetReference()
        {
            return this;
        }

        public void OnAttack()
        {
            Die();
        }
    }
}